# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=jasper
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.900.1
pkgrel=4
pkgdesc="A software-based implementation of the codec specified in the emerging JPEG-2000 Part-1 standard (mingw-w64)"
arch=('any')
url="https://www.ece.uvic.ca/~mdadams/jasper"
license=("custom:JasPer2.0")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config" "dos2unix")
depends=("${MINGW_PACKAGE_PREFIX}-libjpeg-turbo")
options=('staticlibs' 'strip' '!libtool')
source=("https://www.ece.uvic.ca/~mdadams/jasper/software/jasper-${pkgver}.zip"
        patch-libjasper-stepsizes-overflow.diff
        jpc_dec.c.patch
        jasper-1.900.1-CVE-2008-3520.patch
        jasper-1.900.1-CVE-2008-3522.patch
        jasper-pkgconfig.patch
        jasper-1.900.1-CVE-2011-4516-CVE-2011-4517-CERT-VU-887409.patch
        jasper-CVE-2014-9029.patch
        jasper-CVE-2014-8137.patch
        jasper-CVE-2014-8138.patch
        jasper-CVE-2014-8157.patch
        jasper-CVE-2014-8158.patch
        jasper-1.900.1-Coverity-BAD_SIZEOF.patch
        jasper-1.900.1-Coverity-CHECKED_RETURN.patch
        jasper-1.900.1-Coverity-FORWARD_NULL.patch
        jasper-1.900.1-Coverity-NULL_RETURNS.patch
        jasper-1.900.1-Coverity-RESOURCE_LEAK.patch
        jasper-1.900.1-Coverity-UNREACHABLE.patch
        jasper-1.900.1-Coverity-UNUSED_VALUE.patch
        jasper-1.900.1-mingw32.patch
        jasper-1.900.1-enable-shared.patch
        jasper-1.900.1-sleep.patch)
sha256sums=('6b905a9c2aca2e275544212666eefc4eb44d95d0a57e4305457b407fe63f9494'
            'e454f0fb1b994535ca02fa2468aa39ff153a78f3688db3808b6e953c44890e41'
            'fca9c4bddc284d6c59845e5b80adfd670e79c945f166d9624b117c6db0c10492'
            '02236060cae28be5ac46d90ca17ce2de17e975574dd761d9117994e69bdc38d6'
            'b0272ce179ead3692942246523462db33c0f2a92bd9f9a117ff40e8ec963fbac'
            '325003c739023264f368db2ce30598038706f45a8ec8a5a1f81fa855496e0ed1'
            '2d0d69578dbd9cb3331e8bd35595625b5b1db24c078cc0053c550ed0bfdfcc02'
            'a43747e7597a2a5108befd4acd31a582101a66096a752e61de853bc860d2a8e1'
            '27350b9a72067e0325464b1e51f0fcab2701db26c918d82aac977dc345a02999'
            '597966eabef1eeb4155415352cee37492def0abb09349e1764ae92645f3a20c1'
            '60160f1eecb4cbfe7d8277e091333e9c1b4af7eeaccdfa3b539ac9658bb6a474'
            '1dce24d47bcfc599bde5fa625e8b9bfbd1c6c637e4358493276d8a96338ff8b7'
            '5373fdfd7ab272bb516232d01fe3b61b949b62defb0d7ec3d78c6345e0cba01f'
            '3f841f72bb128a961bcd4c14dbcbf8a27d51342b28caa3462555616aa35cd847'
            '961ac4daa5c8621347cd87f9d3cfbee4c1df3c236ecfa4c4cd70a712df2e0234'
            '13126857f4d6c99802a638833615bb90944d4c24c63ff187e56069fd8dae4e1a'
            'e8ec793c80904e353b636ba78410f9232b59fe3a34131cd154794e410f8dc69d'
            '7cd57f19ec109d50a4de1729efe59ebcc7bfe446f233274ab0204c475813286a'
            'ffbc04abd17f7cedd8230b39042def438d1ddde9f885f883b48352b4feb1795a'
            '0f7daf902e14c99fabdd5efa36ed91710253f5b7f277d879ce5c8e0d76bbaa66'
            '64a15c67dd35138e77f07910aa402d0b32ca4148582832a2066dc61eed999f31'
            'ea7984bf14737d2ca79dc89fbf478240937967abc09f142f10e1a6e63d371679')

prepare() {
  cd "$srcdir/jasper-$pkgver"
  find . -type f -exec dos2unix {} \;

  patch -p1 -i "${srcdir}"/patch-libjasper-stepsizes-overflow.diff
  patch -p1 -i "${srcdir}"/jpc_dec.c.patch
  patch -p1 -i "${srcdir}"/jasper-1.900.1-CVE-2008-3520.patch
  patch -p1 -i "${srcdir}"/jasper-1.900.1-CVE-2008-3522.patch
  patch -p1 -i "${srcdir}"/jasper-pkgconfig.patch
  patch -p1 -i "${srcdir}"/jasper-1.900.1-CVE-2011-4516-CVE-2011-4517-CERT-VU-887409.patch
  patch -p1 -i "${srcdir}"/jasper-CVE-2014-9029.patch
  patch -p1 -i "${srcdir}"/jasper-CVE-2014-8137.patch
  patch -p1 -i "${srcdir}"/jasper-CVE-2014-8138.patch
  patch -p1 -i "${srcdir}"/jasper-CVE-2014-8157.patch
  patch -p1 -i "${srcdir}"/jasper-CVE-2014-8158.patch
  patch -p1 -i "${srcdir}"/jasper-1.900.1-Coverity-BAD_SIZEOF.patch
  patch -p1 -i "${srcdir}"/jasper-1.900.1-Coverity-CHECKED_RETURN.patch
  patch -p1 -i "${srcdir}"/jasper-1.900.1-Coverity-FORWARD_NULL.patch
  patch -p1 -i "${srcdir}"/jasper-1.900.1-Coverity-NULL_RETURNS.patch
  patch -p1 -i "${srcdir}"/jasper-1.900.1-Coverity-RESOURCE_LEAK.patch
  patch -p1 -i "${srcdir}"/jasper-1.900.1-Coverity-UNREACHABLE.patch
  patch -p1 -i "${srcdir}"/jasper-1.900.1-Coverity-UNUSED_VALUE.patch
  patch -p1 -i "${srcdir}"/jasper-1.900.1-mingw32.patch
  patch -p1 -i "${srcdir}"/jasper-1.900.1-enable-shared.patch
  patch -p1 -i "${srcdir}"/jasper-1.900.1-sleep.patch

  #libtoolize --copy --force
  #aclocal
  #automake --add-missing
  #autoconf

  autoreconf -fiv
}

build() {
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --enable-static \
    --disable-opengl

  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make check
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
}
